const request = require("miniapp-request");
const store = require('../../libs/storage');

global.Page({

    /**
     * 页面的初始数据
     */
    data: {
        isLoaded: false,
        nick_name: '',
        as_record_count: 0,
    },
    onLoad: function (options) {
        var userInfo = store.get();
        console.log("============index:onLoad");
        console.log(userInfo);
        this.setData({
            nick_name: userInfo.nick_name ? userInfo.nick_name : '',
            as_record_count: userInfo.as_record_count ? userInfo.as_record_count : 0,
        });
        setTimeout(function () {
            // 参加过就跳到地图页面
            var userInfo = store.get();
            if (userInfo.nick_name && userInfo.nick_name.length > 0 && userInfo.as_record_count > 0) {
                wx.navigateTo({
                    url: "/pages/startTest/startTest?nick_name=" + userInfo.nick_name,
                })
            }
        }, 200)
    },
    onShow: function (e) {},
    // 昵称输入
    nickNameChanged(e) {
        console.log(e);
        let nick_name = e.detail.value;
        if (nick_name.length > 0) {
            this.setData({
                nick_name
            });
        }
    },
    // 开始测评
    startTest(e) {
        let nick_name = this.data.nick_name;
        if (nick_name.length > 0) {
            // 保存昵称
            let infos = store.get();
            infos.nick_name = nick_name;
            store.set(infos);

            wx.navigateTo({
                url: "/pages/startTest/startTest?nick_name=" + nick_name,
            })
        } else {
            wx.showToast({
                title: '请先输入您的昵称',
                duration: 2000,
                icon: 'none'
            })
        }
    },

})